package com.ruoyi.web.xxj.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.xxj.domain.Banner;
import com.ruoyi.web.xxj.mapper.BannerMapper;
import com.ruoyi.web.xxj.service.IBannerService;

import io.jsonwebtoken.lang.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * banner轮播图Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-20
 */
@Service
public class BannerServiceImpl implements IBannerService {
    @Autowired
    private BannerMapper bannerMapper;

    /**
     * 查询banner轮播图
     *
     * @param id banner轮播图主键
     * @return banner轮播图
     */
    @Override
    public Banner selectBannerById(Long id) {
        return bannerMapper.selectBannerById(id);
    }

    /**
     * 查询banner轮播图列表
     *
     * @param banner banner轮播图
     * @return banner轮播图
     */
    @Override
    public List<Banner> selectBannerList(Banner banner) {
        return bannerMapper.selectBannerList(banner);
    }

    /**
     * 新增banner轮播图
     *
     * @param banner banner轮播图
     * @return 结果
     */
    @Override
    public int insertBanner(Banner banner) {
    	List<Banner> banners = this.selectBannerList(new Banner(null,banner.getName()));
    	if (!Collections.isEmpty(banners)) {
			throw new ServiceException("已存在名称为["+banner.getName()+"]的轮播图！");
		}
    	banner.setCreateBy(SecurityUtils.getUsername());
        banner.setCreateTime(DateUtils.getNowDate());
        return bannerMapper.insertBanner(banner);
    }

    /**
     * 修改banner轮播图
     *
     * @param banner banner轮播图
     * @return 结果
     */
    @Override
    public int updateBanner(Banner banner) {
    	List<Banner> banners = this.selectBannerList(new Banner(null,banner.getName()));
    	if (!Collections.isEmpty(banners)) {
    		banners.forEach(b -> {
    			if (!b.getId().equals(banner.getId()) && b.getName().equals(banner.getName())) {
    				throw new ServiceException("已存在名称为["+banner.getName()+"]的轮播图！");
				}
    		});
		}
    	banner.setUpdateBy(SecurityUtils.getUsername());
        banner.setUpdateTime(DateUtils.getNowDate());
        return bannerMapper.updateBanner(banner);
    }

    /**
     * 批量删除banner轮播图
     *
     * @param ids 需要删除的banner轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBannerByIds(Long[] ids) {
        return bannerMapper.deleteBannerByIds(ids);
    }

    /**
     * 删除banner轮播图信息
     *
     * @param id banner轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBannerById(Long id) {
        return bannerMapper.deleteBannerById(id);
    }
}
